package cn.study.vue_project_xxx.auth;

import cn.dev33.satoken.stp.StpUtil;
import cn.study.vue_project_xxx.common.result.ResultSet;
import cn.study.vue_project_xxx.common.constants.BaseConstants;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import cn.study.vue_project_xxx.common.constants.BaseConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 认证拦截器
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "auth")
public class AuthIntercept implements WebMvcConfigurer {
    List<String> exclusions;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                boolean flag = StpUtil.isLogin();
                String requestURI = request.getRequestURI();
                if (exclusions.stream().filter(item -> item.equals(requestURI)).count() >= BaseConstants.BaseNumberOne) {
                    flag = true;
                }
                if (!Objects.isNull(request.getParameter("pass")) && request.getParameter("pass").equals("true")) {
                    flag = true;
                }
                if (!flag) {
                    response.setStatus(401);
                    ObjectMapper objectMapper = new ObjectMapper();
                    String result = objectMapper.writeValueAsString(ResultSet.fail("登录未认证,请跳转登录!"));
                    response.getWriter().write(result);
                }
                return flag;
            }

            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
                HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
                HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
            }
        });
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
